import uuid

from CloudEngine.IO.StandardJsonLoader import StandardJsonLoader
from CloudEngine.core.VTActor import VTActor
from Connector.client import res_success
from service.utils.cell_type_helper import get_cell_type_key_type


class CmdRenderHighlight:
    name = "cmd_render_highlight"
    id = uuid.uuid4()

    def __init__(self, service, Params, Tag, Type):
        self.service = service
        self.params = Params
        self.tag = Tag
        self.type = Type

    async def execute(self):
        h_type = self.params.get("type")
        match h_type:
            case "block":
                bid = self.params.get("bid")
                color = self.params.get("color", [255, 0, 0, 255])
                ac: VTActor = self.service.get_first_actor()
                result = ac.loader.blocks_map.get(bid, [])
                if len(result["d2"]) > 0:
                    ac.set_cells_color(result["d2"], color)
                if len(result["d3"]) > 0:
                    d3_ac = self.service.get_third_actor()
                    d3_ac.set_cells_color(result["d3"], color)
                if len(result["line"]) > 0:
                    line_ac = self.service.get_second_actor()
                    line_ac.set_cells_color(result["line"], color)
                self.service.app.render()
            case "cell_type":
                color = self.params.get("color", [255, 0, 0, 255])
                cell_type_key = self.params.get("CellType")
                ac: VTActor = self.service.get_first_actor()
                ac_type = get_cell_type_key_type(cell_type_key)
                loader: StandardJsonLoader = ac.loader
                print(cell_type_key, ac_type)
                index_arr = loader.type_map[cell_type_key]
                match ac_type:
                    case "d2":
                        ac.set_cells_color(index_arr, color)
                    case "line":
                        line_ac = self.service.get_second_actor()
                        line_ac.set_cells_color(index_arr, color)
                    case "d3":
                        d3_ac = self.service.get_third_actor()
                        d3_ac.set_cells_color(index_arr, color)
                self.service.app.render()

        await res_success(self.tag, self.type, {"status": "ok"})
